use pkt_get::common::log::init_logger;
use pkt_get::packet_hdr::NetworkPacket;
use pkt_get::pcap_parse::pcap_file::PcapFile;
use std::fs::File;
use std::io::Read;

#[test]
fn test_parse_pcap_file1() {
    init_logger();
    let mut pcap_file = PcapFile::new("tests/test-1.pcap").unwrap();
    pcap_file.build_index_from_pcap().unwrap();

    for pcap_index in 0..pcap_file.get_index_num() {
        let network_packet = NetworkPacket::new_from_pcap(pcap_index, &mut pcap_file);
        match network_packet {
            Ok(ret) => {
                println!("{}", ret);
                assert_eq!(pcap_index, pcap_index);
                pcap_file.cache(pcap_index, ret);
            }
            Err(e) => {}
        }
    }

    let mut ok_string = String::new();
    let mut file = File::open("tests/test_pcap_ok-1.txt").unwrap();
    file.read_to_string(&mut ok_string).unwrap();

    assert_eq!(format!("{:?}", pcap_file.get_pcap_index_arr()), ok_string);
}

#[test]
fn test_parse_pcap_file2() {
    let mut pcap_file = PcapFile::new("tests/test-2.pcap").unwrap();
    pcap_file.build_index_from_pcap().unwrap();

    for pcap_index in 0..pcap_file.get_index_num() {
        let network_packet = NetworkPacket::new_from_pcap(pcap_index, &mut pcap_file);
        match network_packet {
            Ok(ret) => {
                println!("{}", ret);
                pcap_file.cache(pcap_index, ret);
            }
            Err(e) => {}
        }
    }

    let mut ok_string = String::new();
    let mut file = File::open("tests/test_pcap_ok-2.txt").unwrap();
    file.read_to_string(&mut ok_string).unwrap();

    assert_eq!(format!("{:?}", pcap_file.get_pcap_index_arr()), ok_string);
}
